<template>
  <div class="goods">
    <van-swipe :autoplay="3000" class="goods-swipe">
      <van-swipe-item v-for="item in imgList" :key="item.id">
        <img :src="item.src" />
      </van-swipe-item>
    </van-swipe>

    <van-cell-group v-for="item in goods" :key="item.id">
      <van-cell>
        <div class="goods-title">{{ item.title }}</div>
      </van-cell>
      <div class="new">
        <span>
          市场价:
          <s>￥{{item.market_price}}</s>
        </span>
        <span>
          销售价:
          ￥:{{item.sell_price}}
        </span>
        <p>
          <span class="may">购买数量</span>
          <van-stepper v-model="goodsNum" />
        </p>
        <van-button type="info" size="normal">立即购买</van-button>
        <van-button type="danger" size="normal" @click="joinCar(item.id)">加入购物车</van-button>
      </div>
      <div class="xx">
        <van-cell>商品参数</van-cell>
        <p>
          商品货号:
          {{item.goods_no}}
        </p>
        <p>
          库存情况:
          {{item.stock_quantity}}件
        </p>
        <p>
          上架时间:
          {{item.add_time | dateFormat}}
        </p>
        <p>
          <van-button plain type="info" @click="goodsDesc(item.id)">图文介绍</van-button>
        </p>
        <p>
          <van-button plain type="danger" @click="goodComment(item.id)">商品评论</van-button>
        </p>
      </div>
    </van-cell-group>
  </div>
</template>
<script>
export default {
  data() {
    return {
      goods: {},
      imgList: [],
      goodsNum: 1
    };
  },
  mounted() {
    this.getGoods();
    this.getimgList();
  },
  methods: {
    async getGoods() {
      const { data: res } = await this.$http.get(
        "/api/goods/getinfo/" + this.$route.params.id
      );
      // console.log(res);
      this.goods = res.message;
    },
    async getimgList() {
      const { data: res } = await this.$http.get(
        "/api/getthumimages/" + this.$route.params.id
      );
      this.imgList = res.message;
    },
    formatPrice() {
      return "¥" + (this.goods.price / 100).toFixed(2);
    },
    onClickCart() {
      this.$router.push("cart");
    },
    goodsDesc(id) {
      this.$router.push("/goodsdesc/" + id);
    },
    goodComment(id) {
      this.$router.push("/comment/" + id);
    },
    // 加入购物车
    joinCar(id) {
      const params = {id, num: this.goodsNum}
      this.$store.commit('joinCar', params);
      this.$toast.success('已加入购物车');
    }
  }
};
</script>
<style lang='less' scoped>
.van-swipe {
  margin: 50px 10px 10px 10px;
  height: 230px;
  border: 1px solid #ccc;
  border-radius: 3px;
  .van-swipe-item {
    display: flex;
    justify-content: center;
    img {
      width: 200px;
      height: 200px;
      margin: auto;
    }
  }
}
.van-cell-group {
  height: 200px;
  border: 1px solid #ccc;
  margin: 10px;
  .van-cell {
    font-size: 16px;
    color: black;
  }
}
.new {
  margin-left: 30px;
  font-size: 14px;
  color: rgb(134, 133, 133);
  .may {
    float: left;
    margin-right: 10px;
  }
  .van-button {
    padding: 10px;
    height: 33px;
    text-align: center;
    line-height: 10px;
    margin-right: 10px;
  }
  .bb {
    border: 1px solid #ccc;
  }
}
.xx {
  margin: 60px 0px;
  height: 280px;
  width: 100%;
  border: 1px solid #ccc;
  p {
    font-size: 14px;
    color: rgb(134, 133, 133);
    padding: 0px 10px;
    .van-button {
      width: 100%;
    }
  }
}
</style>